/*
* filePath 请求文件路径
*/
import { baseUtils } from './base.js';

const $uplaod = (filePath) =>{
	let token = uni.getStorageSync('token') || '';
	let user = uni.getStorageSync('userInfo') || '';
	/// 默认
	return new Promise((resolve, reject)=>{
		uni.showLoading({
			title:'上传中...',
			mask:true
		});
		let fileUrl = [];
		uni.uploadFile({
			url:baseUtils.baseUrl()+'/api/auth/uploadFile/upload',
			filePath:filePath,
			header:{
				"token":token,
				"user_id": (user && user != '') ? user.userId : ''
			},
			name:'file',
			success:function(res){
				uni.hideLoading();
				let data = JSON.parse(res.data);
				if(data.code == '0'){
					let url = data.data;
					resolve(url)
				}else{
					uni.showToast({
						title:data.msg,
						icon:'none',
						duration:2000
					})
					reject(data)
				}

			},
			fail:function(e){
				uni.hideLoading();
				uni.showToast({
					title:'上传失败',
					icon:'none',
					duration:2000
				})
				reject(e)
			}
		})
	});
}

export default $uplaod;
